package com.cyy.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cyy.bean.user;
import com.cyy.util.dbUtil;

public class userDao {
//    注册之后添加一个用户
   public boolean addUser(String name,String pwd,String email,String dateTime) throws SQLException, ClassNotFoundException {
       Connection conn = dbUtil.getConnection();
       String sql="insert into user values(?,?,?,?,?,?)";
       PreparedStatement pstmt=conn.prepareStatement(sql);
       pstmt.setString(1,name);
       pstmt.setString(2,pwd);
       pstmt.setString(3,email);
       pstmt.setString(4,dateTime);
       pstmt.setString(5,"未填");
       pstmt.setString(6,"未填");
       int i = pstmt.executeUpdate();
       dbUtil.close(null,pstmt,conn);
       if(i==1)
       {
           return true;
       }
       return false;
   }
//   判断该用户是否已经注册
    public boolean selectNamePwd(String name,String pwd) throws SQLException, ClassNotFoundException {
        Connection conn = dbUtil.getConnection();
        String sql="select * from user where name=? and pwd=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
//        设置参数
        pstmt.setString(1,name);
        pstmt.setString(2,pwd);
//        查询数据库
        ResultSet resultSet = pstmt.executeQuery();
        if(resultSet.next())
        {
            if(resultSet.getString("name")==null||resultSet.getString("pwd")==null)
            {
                dbUtil.close(resultSet, pstmt, conn);
                return false;
            }
            else
            {
                dbUtil.close(resultSet, pstmt, conn);
                return true;
            }
        }
        return false;
    }

//    根据用户名得到用户邮箱
//    public String selectEmail(String name) throws SQLException, ClassNotFoundException {
//        Connection conn=dbUtil.getConnection();
//        String sql="select email from user where name=?";
//        PreparedStatement pstmt =conn.prepareStatement(sql);
//        pstmt.setString(1,name);
//        ResultSet resultSet = pstmt.executeQuery();
//        if(resultSet.next()) {
//            return resultSet.getString("email");
//        }
//        return "";
//    }


//  根据用户名得到用户基本信息
    public user selectAll(String userName) throws SQLException, ClassNotFoundException {
        Connection conn=dbUtil.getConnection();
        String sql="select * from user where name=?";
        PreparedStatement pstmt =conn.prepareStatement(sql);
        pstmt.setString(1,userName);
        ResultSet resultSet = pstmt.executeQuery();
        if(resultSet.next()) {
            String registerTime=resultSet.getString("registerTime");
            String pwd=resultSet.getString("pwd");
            String email=resultSet.getString("email");
            String address=resultSet.getString("address");
            String phone=resultSet.getString("phone");
            return new user(userName,pwd,email,registerTime,address,phone);
        }
        return null;
    }

//  修改用户基本信息
    public void update(String userName, String pwd,String phone,String adddress) throws SQLException, ClassNotFoundException {
        Connection conn = dbUtil.getConnection();
        String sql = "update user set phone=?,address=?,pwd=? where name=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, phone);
        pstmt.setString(2, adddress);
        pstmt.setString(3, pwd);
        pstmt.setString(4, userName);
        int i = pstmt.executeUpdate();
        dbUtil.close(null,pstmt,conn);
    }
}
